//
//  UIImageView+MZCategory.m
//  MZToolkitDemo
//
//  Created by 沈家林 on 2018/6/5.
//  Copyright © 2018年 Chris Yang. All rights reserved.
//

#import "UIImageView+MZCategory.h"
#import "CAAnimation+MZCategory.h"

@implementation UIImageView (MZCategory)

- (void)mzSetImageWithImageUrl:(NSString *)imageUrl withResultBlock:(MZLoadingResult)resultBlock{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (data) {
                if ([imageUrl hasSuffix:@"gif"]) {
                    CAAnimation *animation=[CAAnimation mzGifAnimationWithGifData:data];
                    animation.fillMode=kCAFillModeForwards;
                    animation.removedOnCompletion = NO;
                    animation.repeatCount=INFINITY;
                    self.image = nil;
                    [self.layer addAnimation:animation forKey:@"mzCommon"];
                }else{
                    self.image=[UIImage imageWithData:data];
                }
                resultBlock(true);
            }else{
                resultBlock(false);
            }
        });
    });
}

@end
